//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.qy137.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qy137.entity.Zone;
import com.qy137.vo.ZoneVo;
import org.apache.commons.lang.StringUtils;
import org.qy137.dao.ZoneDao;
import org.qy137.service.ZoneService;
import org.qy137.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ZoneServiceImpl implements ZoneService {
    @Autowired
    private ZoneDao zoneDao;

    public ZoneServiceImpl() {
    }

    public CommonResult getAllZone(Integer page, Integer limit, ZoneVo zoneVo) {
        Page<Zone> zonePage = new Page((long)page, (long)limit);
        QueryWrapper<Zone> wrapper = new QueryWrapper();
        wrapper.eq("is_deleted", 0);
        if (StringUtils.isNotEmpty(zoneVo.getZoneName())) {
            wrapper.like("zone_name", zoneVo.getZoneName());
        }

        if (zoneVo.getIsDisable() != null) {
            wrapper.eq("is_disable", zoneVo.getIsDisable());
        }

        Page<Zone> selectPage = (Page)this.zoneDao.selectPage(zonePage, wrapper);
        return selectPage.getRecords().size() > 0 ? new CommonResult(2000, "查询成功", selectPage) : new CommonResult(5000, "查询失败", (Object)null);
    }

    public CommonResult changeStatus(String id, Integer isDisable) {
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDisable(isDisable);
        int i = this.zoneDao.updateById(zone);
        return i > 0 ? new CommonResult(2000, "修改成功", (Object)null) : new CommonResult(5000, "修改失败", (Object)null);
    }

    public CommonResult deleteZone(String id) {
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDisable(1);
        int i = this.zoneDao.updateById(zone);
        return i > 0 ? new CommonResult(2000, "删除成功", (Object)null) : new CommonResult(5000, "删除失败", (Object)null);
    }

    public CommonResult addZone(ZoneVo zoneVo) {
        if (StringUtils.isNotEmpty(zoneVo.getId())) {
            Zone zone = new Zone();
            zone.setId(zoneVo.getId());
            zone.setZoneName(zoneVo.getZoneName());
            zone.setZoneDesc(zoneVo.getZoneDesc());
            int i = this.zoneDao.updateById(zone);
            return i > 0 ? new CommonResult(2000, "修改成功", (Object)null) : new CommonResult(5000, "修改失败", (Object)null);
        } else {
            QueryWrapper<Zone> wrapper = new QueryWrapper();
            wrapper.eq("zone_name", zoneVo.getZoneName());
            Zone zone = (Zone)this.zoneDao.selectOne(wrapper);
            if (zone != null) {
                return new CommonResult(5000, "名字触发不能添加", (Object)null);
            } else {
                Zone zone1 = new Zone();
                zone1.setZoneName(zoneVo.getZoneName());
                zone1.setZoneDesc(zoneVo.getZoneDesc());
                zone1.setIsDisable(0);
                zone1.setIsDeleted(0);
                int insert = this.zoneDao.insert(zone1);
                return insert > 0 ? new CommonResult(2000, "添加成功", (Object)null) : new CommonResult(5000, "添加失败", (Object)null);
            }
        }
    }
}
